﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Bll;
using CSP.Bll.IObjectBlls;
using CSP.Common.Web;
using CSP.Web.Master;
using CSP.Web.UserControls;

namespace CSP.Web.MyHome.Authentication
{
    public partial class CreateAuthentication : CSPMyHome
    {
        /// <summary>
        /// 认证类型 1个人认证，2企业认证
        /// </summary>
        private int _authenticationType;

        protected void Page_Load(object sender, EventArgs e)
        {
            //指定被选中的导航
            ((HomeMaster)Master).SelectedNavigationLink = MyHomeLeftNavigationLink.Authentication;

            InitAuthenticationType();
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            AddAuthentication();
        }

        /// <summary>
        /// 初始化要认证的类型
        /// </summary>
        private void InitAuthenticationType()
        {
            _authenticationType = Convert.ToInt32(Request.QueryString["type"]);
            if (_authenticationType != 0 && _authenticationType != 1)
            {
                Response.Write("<script>alert('认证类型不正确')</script>");
                this.FailRedirect("认证类型不正确", "List.aspx");
            }

            ListItem removeItem = null;
            if (_authenticationType == 0)
            {
                removeItem = this.S_CertificateType.Items.FindByValue("1");
                LB_AuthenticationTitle.Text = "机构认证";
            }
            else if (_authenticationType == 1)
            {
                removeItem = this.S_CertificateType.Items.FindByValue("0");
                LB_AuthenticationTitle.Text = "个人认证";
            }

            if (removeItem != null)
            {
                S_CertificateType.Items.Remove(removeItem);
            }
        }

        /// <summary>
        /// 获取证件信息
        /// </summary>
        /// <returns></returns>
        private CSP.Model.CSP.Authentication GetAuthenticationInfo()
        {
            CSP.Model.CSP.Authentication authentication = new Model.CSP.Authentication
            {
                Name = TxtName.Value,
                Status = 0,
                CertificateType = _authenticationType,
                Username = UserProvider.UserName
            };

            return authentication;
        }

        /// <summary>
        /// 创建认证
        /// </summary>
        private void AddAuthentication()
        {
            if (this.TxtUpload.PostedFile.ContentLength>614400)
            {
                this.FailRedirect("抱歉！上传的认证图片不能大于500KB,请压缩图片后再上传", "/MyHome/Authentication/List.aspx");
            }

            CSP.Model.CSP.Authentication authentication = GetAuthenticationInfo();

            CSPBllFactory factory = CSPBllFactory.GetInstance();
            using (IAuthenticationBll authenticationBll = factory.GetAuthenticationBll())
            {
                try
                {
                    authentication = authenticationBll.Add(authentication, this.TxtUpload.PostedFile);

                }
                catch (Exception ex)
                {
                    LoggerProvider.Error("adsafsad", ex);
                    this.FailRedirect("提交失败", "/MyHome/Authentication/List.aspx");
                }
            }
            LoggerProvider.Error("adsafsad", new Exception("提交成功"));
            this.SuccessfullRedirect("已经提交成功", "/MyHome/Authentication/List.aspx");
        }



    }
}